package com.hk.service.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hk.commons.entity.Fundnavhistory;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IFundnavHistory;

@Service
public class FundnavhistoryImpl extends BaseServiceImpl<Fundnavhistory>
		implements IFundnavHistory {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public Fundnavhistory findById(Integer id) {

		return getById(id);
	}

	@Override
	public Integer add(Fundnavhistory u) {
		return (Integer) super.save(u);
	}

	@Override
	public void updateBatch(List<Fundnavhistory> list) {
		final List<Fundnavhistory> tempList = list;
		String sql = "insert into t_fund_nav_history(id , accumulatedNav , dayReturn , fundCode , nav , navDate , unitYield , yearlyRoe)"
				+ "values(?, ? , ? , ? , ? , ? , ? , ?)";

		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
				ps.setInt(1, tempList.get(i).getId());
				ps.setBigDecimal(2, tempList.get(i).getAccumulatedNav());
				ps.setBigDecimal(3, tempList.get(i).getDayReturn());
				ps.setString(4, tempList.get(i).getFundCode());
				ps.setBigDecimal(5, tempList.get(i).getNav());
				ps.setString(6, tempList.get(i).getNavDate());
				ps.setBigDecimal(7, tempList.get(i).getUnitYield());
				ps.setBigDecimal(8, tempList.get(i).getYearlyRoe());
			}

			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});

	}

}
